
function randomSet() { //随机生成17位;实际上生成了18位
	let firstLine = [1, 2, 3, 4, 5, 6, 7, 8, 9];
	let secondLine = [1, 2, 3, 4, 5, 6, 7, 8, 9]
	let x;
	let t;
	for (let i = 0; i < 9; i++) {
		x = Math.floor(Math.random() * 100 % 9);
		t = firstLine[i];
		firstLine[i] = firstLine[x];
		firstLine[x] = t;
	}
	for (let i = 0; i < 9; i++) {
		x = Math.floor(Math.random() * 100 % 9);
		t = secondLine[i];
		secondLine[i] = secondLine[x];
		secondLine[x] = t;
	}
	for (let i = 0; i < 9; i++) {
		if (firstLine[i] == secondLine[i]) {
			if (i != 8) {
				t = secondLine[i]
				secondLine[i] = secondLine[i + 1];
				secondLine[i + 1] = t;
			} else {
				t = secondLine[i]
				secondLine[i] = secondLine[0];
				secondLine[0] = t;
			}
		}
	}
	return [firstLine, secondLine];

}
function setSudoArr(towlines) {//创建一个数独数租
	let arr = new Array();
	for (let i = 0; i < 9; i++) {
		arr.push(new Array());
		for (let k = 0; k < 9; k++) {
			arr[i][k] = 0;
		}
	}
	arr[0] = towlines[0];
	arr[3] = towlines[1];
	return arr;
};
function testing(arr) { //测试数独盘是否有重复 1为有重复0为安全
	for (let i = 0; i < 9; i++) {
		for (let k = 0; k < 9; k++) {
			if (arr[i][k] != 0) {
				for (let j = k + 1; j < 9; j++) {
					if (arr[i][k] == arr[i][j]) {
						return 1;
					}
				}
				for (let j = i + 1; j < 9; j++) {
					if (arr[i][k] == arr[j][k]) {
						return 2;
					}
				}
				for (let x =i- i%3; x <i- i%3 + 3; x++) {
					for (let y =k- k%3 ; y <k- k%3 + 3; y++) {
						if(i==x&&k==y){
							continue;
						}
						if (arr[i][k] == arr[x][y]) {
							return 3;
						}
					}
				}
			}
		}
	}
	return 0;
}
function testAnswer(arr) { //由给出的数组通过穷举测出最终答案
	
	return tryALL(0,0);
	function tryALL(x,y){
		if(y>=9){
			y=0;x++;
		}
		if(x>=9){
			return arr;
		}
		if(arr[x][y]==0){
			for(let i=1;i<=9;i++){
				arr[x][y]=i;
				if(testing(arr)==0){
					if(tryALL(x,y+1)){
						return arr;
					}
				}
			}
			arr[x][y]=0;
		}else{
			if(tryALL(x,y+1)){
				return arr;
			}
		}
		
		return 0;
		
	}
	
}
function getRandomSudo(){//返回一个随机完整的数独
	return testAnswer(setSudoArr(randomSet()));
}
 
 
 